// app/http/middleware/language.go
package middleware

import (
	"strings"

	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
)

func Language() http.Middleware {
	return func(ctx http.Context) {
		// 获取 Accept-Language 请求头
		acceptLanguage := ctx.Request().Header("Accept-Language", "en")

		// 设置语言
		var lang string
		if strings.Contains(acceptLanguage, "zh") {
			lang = "zh" // 或者 "cn"
		} else {
			lang = "en"
		}

		// 设置语言环境
		facades.App().SetLocale(ctx, lang)

		facades.Log().Info("Language set to: " + lang)

		// 继续处理请求
		ctx.Request().Next()
	}
}
